package org.wicket.jbpm.task.pages.cmplxprocess;

import java.util.List;
import java.util.Stack;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicket.jbpm.engine.spring.JbpmProcessEngine;
import org.wicket.jbpm.engine.utils.JbpmVars;
import org.wicket.jbpm.engine.workflow.WfTask;
import org.wicket.jbpm.page.BaseProcessPage;

/**
 * 
 * @author Emmanuel Nollase - emanux
 * @created 2009 11 5 - 09:35:04
 *
 */
public class JbpmVarPage extends BaseProcessPage {

	@SpringBean private JbpmProcessEngine jbpmProcessEngine;
	
	public JbpmVarPage(final WfTask wfTask) {
		super(wfTask);

		Stack<JbpmVars> taskvars = jbpmProcessEngine.getJbpmVars(wfTask);
		
		final Form form = new Form("form");
		add(form);
		
		final ListView vars = new ListView("vars", taskvars ) {
			
			@Override
			protected void populateItem(ListItem item) {
				final JbpmVars jbpmvars = (JbpmVars) item.getModelObject();
				item.setModel(new CompoundPropertyModel(jbpmvars));
				item.add(new Label("jbpmVars"));
				item.add(new TextField("jbpmValue"));
			}
		};
		
		form.add(vars);
		
		final IndicatingAjaxButton submit = new IndicatingAjaxButton("submit", form) {
			
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form form) {
				final List<JbpmVars> getvars = (List<JbpmVars>) vars.getModelObject();
				
				//  Do whatever you want with the value from the variables
				for(JbpmVars vars : getvars)
				{
					System.out.println(vars.getJbpmValue());
				}
				
			}
		};
		
		form.add(submit);
	}

	
}

